package e.g.com.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import e.g.com.dao.BaseDao;
import e.g.com.pojo.Tb_topic;
import e.g.com.pojo.TopBianJi;
import e.g.com.service.TopicService;

@Controller
public class TopicController {
	@Resource
	private TopicService tps;
	@Resource 
	private BaseDao<Tb_topic>bd;
	@RequestMapping(value="/manage/tpyl",method=RequestMethod.GET)
	public String topyuelan(@RequestParam("pagenow")int pagenow,Model m){
		List<Tb_topic>list=tps.findpage(pagenow);
		int cout=tps.findall().size();
		int size;
		if(cout<10)size=1;
		else if(cout%10==0)size=cout/10-1;
		else size=cout/10;
		m.addAttribute("page", pagenow);
		m.addAttribute("top", list);
		m.addAttribute("size", size);
		return"/manage/topyuelan";
	}
	@RequestMapping(value="/manage/tbianji",method=RequestMethod.GET)
	public String topbianji(@RequestParam("id")int id,Model m){
		Tb_topic topic=tps.findbyid(id);
		m.addAttribute("content", topic.getTopic_content());
		m.addAttribute("answer", topic.getAnswer());
		m.addAttribute("sub", topic.getSubject_id());
		m.addAttribute("type", topic.getType_id());
		m.addAttribute("id", id);
		return"/manage/topbianji";
	}
	@RequestMapping(value="/manage/tbianjiin",method=RequestMethod.POST)
	public String topbianjiin(TopBianJi t,Model m){
		Tb_topic top=new Tb_topic();
		top.setTopic_id(t.getId());
		top.setTopic_content(t.getTopic());
		top.setAnswer(t.getAnswer());
		top.setSubject_id(t.getSubject());
		top.setType_id(t.getType());
		tps.update(top);
		return topyuelan(0, m);
	}
	@RequestMapping(value="manage/tadd",method=RequestMethod.GET)
	public String topadd(TopBianJi t,Model m){
		Tb_topic top=new Tb_topic();
		top.setTopic_id(t.getId());
		top.setTopic_content(t.getTopic());
		top.setAnswer(t.getAnswer());
		top.setSubject_id(t.getSubject());
		top.setType_id(t.getType());
		tps.insert(top);
		return topyuelan(0, m);
	}
}